/*
 * @Author: wuqingchun
 * @Date: 2024-04-20 22:35:30
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-04-22 20:16:20
 */
#include <iostream>
#include "class_base.h"

// 非只读静态成员变量需要在类外进行初始化
unsigned int ClassBase::m_uiDerivedCount = 0;

std::atomic<unsigned int> ClassBase::m_uiAtomicDerivedCount_public {0};
std::atomic<unsigned int> ClassBase::m_uiAtomicDerivedCount_protected {0};
std::atomic<unsigned int> ClassBase::m_uiAtomicDerivedCount_private {0};

ClassBase::ClassBase()
{
    std::cout << "ClassBase::ClassBase()" << std::endl;
    m_uiAtomicDerivedCount_public.fetch_add(1);
    m_uiAtomicDerivedCount_protected.fetch_add(1);
    m_uiAtomicDerivedCount_private.fetch_add(1);
}

ClassBase::ClassBase(const ClassBase& refCls)
{
    std::cout << "ClassBase::ClassBase(const ClassBase& refCls)" << std::endl;
}

ClassBase::ClassBase(ClassBase&& refCls)
{
    std::cout << "ClassBase::ClassBase(ClassBase&& refCls)" << std::endl;
}

ClassBase::~ClassBase()
{
    std::cout << "ClassBase::~ClassBase()" << std::endl;
}

ClassBase& ClassBase::operator=(const ClassBase& refCls)
{
    std::cout << "ClassBase::operator=(const ClassBase& refCls)" << std::endl;
}

void ClassBase::SetName(const std::string& refName)
{
    // 类的私有成员(成员函数和成员变量)只能在类内部访问
    this->__SetName(refName);
}

const std::string& ClassBase::GetName() const
{
    return m_strName;
}

void ClassBase::ShowInfo() const
{
    std::cout << "ClassBase::ShowInfo()" << std::endl;

    std::cout << "Name: " << m_strName << std::endl;
}


void ClassBase::__SetName(const std::string& refName)
{
    m_strName = refName;
}


void ClassBase::ShowDerivedCount()
{
    std::cout << "======ClassBase=====" << std::endl;
    std::cout << "    public: " << m_uiAtomicDerivedCount_public.load() << std::endl;
    std::cout << " protected: " << m_uiAtomicDerivedCount_protected.load() << std::endl;
    std::cout << "   private: " << m_uiAtomicDerivedCount_private.load() << std::endl;

    // 静态成员函数不属于任何类对象，没有隐式参数this，无法访问类的非静态成员变量和成员函数
    // error: invalid use of member ‘ClassBase::m_strName’ in static member function
    // std::cout << "Name: " << m_strName << std::endl;

    // error: cannot call member function ‘const string& ClassBase::GetName() const’ without object
    // std::cout << "Name: " << GetName() << std::endl;
}